/*****************************************************************************/
/* File: SoundManager.h					                                     */
/* Author: rszulgo                                                           */
/*                                                                           */
/* Description:																 */
/* The sound manager avoid a same sound to be loaded multiple				 */
/* times. It keeps a map containing all the already loaded sounds.			 */
/*****************************************************************************/
#pragma once

#include "bass.h"
#include <map>
#include <string>

class CSoundManager
{
public:
	// Loads a sound specified by its filename. If the sound is not
	// loaded already, the sound manager will load it, store it and 
	// return it. Otherwise it simply returns the existing one.
	bool AddSound(const std::string& strSoundName);
	
	// Release the sound specified by its filename. Returns true if 
	// the sound was found, otherwise false.
	bool ReleaseSound(const std::string& strSoundName);
	
	// Play/Stop this sound with openGL: this sound becomes
	// the 'active'/'passive' sound in openGL.
	BOOL PlaySound(const std::string& strSoundName, bool loop);
	BOOL StopSound(const std::string& strSoundName);
	void RestartSound(const std::string& strSoundName,bool loop);
	
	// Returns the single instance of the sound manager.
	// The manager is implemented as a singleton.
	static CSoundManager* GetInstance();
	

protected:
	CSoundManager(void);
	~CSoundManager(void);

private:
	int LoadFile(const std::string& strFileName);
	
	// Error msg
	void Error(const char *es);
	
	// Structure that contains the information about the sound.
	struct SSoundData
	{
		// sound stream
		HSTREAM *strs;
		// sound channel
		DWORD dwChan;
	};

	// Reference count of the number of sound that still hold a map of sounds
	int m_iRefCount;

	typedef std::map<std::string,SSoundData*> TSoundMap;
	
	// The map of already loaded sounds. There are indexed
	// using their filename.
	TSoundMap m_mSounds;
};
